/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package swym.protocol.server;

import swym.protocol.*;
import swym.server.*;

import java.io.*;
import java.util.*;
import java.net.*;

/**
 *
 * @author mschaefer
 */
public class ConnectedClientsQueryMessageHandler extends MessageHandler
{
    public ConnectedClientsQueryMessageHandler( ActionMessage message ) 
            throws MessageException
    {
        super( message );
    }
    
    /**
     * reply with a confirmation message containing a HashMap with
     * the client names and their connection status.
     * @param socket
     * @throws java.io.IOException
     * @throws swym.protocol.CloseConnectionException
     */
    public void handleMessage( ClientSocket socket ) throws IOException, CloseConnectionException
    { 
        /*
         * get connected clients
         * place in confirmation message (send as payload)
         */
        Vector<Client> allClients = user.getClients();
        
        HashMap<String, Boolean> connectionTable = new HashMap<String, Boolean>();
        
        for ( Client client : allClients )
            connectionTable.put( client.getName(), new Boolean( client.isConnected() ) );
        
        ConfirmActionMessage message = new ConfirmActionMessage( user.getName(),
                                                                 client.getName(), 
                                                                 requestID );
        message.setPayload( connectionTable );
        
        writeMessage( socket, message );
    }
}
